<template>
  <div id="ww-appchats">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb">
        <li class="breadcrumb-item">企业微信</li>
        <li class="breadcrumb-item active" aria-current="page">群聊会话</li>
        <span class="ms-auto" @click.stop="back()">返回</span>
      </ol>
    </nav>

    <div class="card col-12 mt-4 px-0">
      <div class="card-body">
        <input
          type="text"
          class="form-control"
          v-model.trim="name"
          placeholder="会话名称"
        />
        <template v-for="info in users">
          <input
            type="checkbox"
            :value="info.userId"
            v-model="selectedIds"
          />({{ info.userId }}){{ info.name }}
        </template>

        <button
          type="button"
          class="btn btn-primary"
          @click.stop.prevent="createAppChat()"
        >
          保存
        </button>
      </div>
      <table class="table table-sm table-striped">
        <thead>
          <tr>
            <th>名称</th>
            <th>chat_id</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="info in dataList" :key="`app_chat_` + info.id">
            <td>
              {{ info.name }}
            </td>
            <td>{{ info.chatId }}</td>
            <td>
              <button
                type="button"
                class="btn btn-primary"
                @click.stop.prevent="sendAppChat(info.chatId)"
              >
                测试发送
              </button>
              <button
                type="button"
                class="btn btn-primary"
                @click.stop.prevent="searchAppChat(info.chatId)"
              >
                刷新
              </button>
            </td>
          </tr>
        </tbody>
      </table>
      <nav id="pagination-box" class="float-end">
        <my-pagination
          name="pagination"
          :row-count="sc.rowCount"
          :page-total="sc.pageTotal"
          :page-no="sc.pageNo"
          @next-page="nextPage"
          @prev-page="prevPage"
          @direct-page="directPage"
        ></my-pagination>
      </nav>
    </div>
  </div>
</template>

<script>
import {
  searchAppChats,
  searchAppChat,
  createAppChat,
  searchUsers,
  sendAppChat,
} from "@/api/workweixin.js";
import { convertLongToTimeDesc } from "@/common/common.js";
import MyPagination from "@/components/my-pagination_h5.vue";

export default {
  components: {
    MyPagination,
  },
  data() {
    return {
      dataList: [],
      name: "",
      users: [],
      selectedIds: [],

      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
    };
  },
  mounted: function () {
    this.search();
    this.searchUsers();
  },
  methods: {
    back: function () {
      this.$router.go(-1);
    },
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", { loading: false });
    },
    search: function () {
      this.showLoading();

      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
      };

      searchAppChats(
        params,
        (v) => {
          this.dataList = v.dataList;
          this.sc = v.page;
        },
        () => this.hideLoading()
      );
    },
    searchUsers: function () {
      this.showLoading();

      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
      };

      searchUsers(
        params,
        (v) => {
          this.users = v.dataList;
          this.sc = v.page;
        },
        () => this.hideLoading()
      );
    },
    createAppChat: function () {
      this.showLoading();

      var params = {
        name: this.name,
        users: this.selectedIds,
      };

      createAppChat(
        params,
        (jsonResult) => {
          if (jsonResult.status === "OK") {
            this.showErrMsg("已保存");
            this.search();
          } else {
            this.showErrMsg("保存不成功", "danger");
          }
        },
        () => this.hideLoading()
      );
    },
    sendAppChat: function (chatId) {
      var params = {
        chatId: chatId,
        content: "test by yfdai",
      };

      sendAppChat(
        params,
        (jsonResult) => {
          if (jsonResult.status === "OK") {
            this.showErrMsg("操作完成");
          } else {
            this.showErrMsg("不成功", "danger");
          }
        },
        () => this.hideLoading()
      );
    },
    searchAppChat: function (chatId) {
      var params = {
        chatId: chatId,
      };

      searchAppChat(
        params,
        (v) => {
          if (v !== null && v.chatId === chatId) {
            this.search();
          }
        },
        () => this.hideLoading()
      );
    },
    reset: function () {
      this.editing = false;
      this.name = "";
      this.orderNum = 0;
      this.level = 1;
      this.parentId = 0;
      this.type = "";
      this.url = "";
      this.key = "";
    },
    formatDateTime: function (val) {
      return convertLongToTimeDesc(val);
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
